<template>
  <!-- element-UI 级联组件 -->
  <el-cascader
   size="mini"
   :options="treeDara"
   :props="props"
     separator="-"
     :value="value"
     @change="changeValue"
   />
  
</template>
<script>
import { getDepartment} from '@/api/department'
import { transListToTreeData} from '@/utils'
export default{
  props:{
    value:{
      type:Number, //存储的是部门的id
      default:null
    }
  },
  data() {
    return {
      treeDara:[],  //赋值给 级联组件的options
      props:{
        label:'name', //要展示的字段
        value:'id'  //要存储的字段
      }
    }
  },
  created() {
    this.getDepartment()
  },
  methods:{
    async getDepartment() {
      this.treeDara = transListToTreeData(await getDepartment(),0)  //将组织架构的数据 转化树形赋值给treeData
    },
    changeValue(list) {
      // 渠道数组的最后一位
      if(list.length > 0) {
        this.$emit('input',list[list.length -1])
      }else {
        this.$emit('input',null)  //如果长度为0 说明值为空
      }
    }
  }
}

</script>